﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
// MakePanoForGoogle, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace MakePanoForGoogle
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //====================================================================================================================
        internal bool showFile(string fileName)
        {
            bool retVal = true; ;
            try
            {
                image1.Visibility = System.Windows.Visibility.Hidden;
                image1.Refresh();
                win1.IsEnabled = false;
                win1.Cursor = Cursors.Wait;
                //
                BitmapImage myBitmapImage = new BitmapImage();
                // BitmapImage.UriSource must be in a BeginInit/EndInit block
                
                myBitmapImage.BeginInit();
                myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                myBitmapImage.UriSource = new Uri(fileName);//, new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore));
                //myBitmapImage.DecodePixelWidth = (int)image1.ActualWidth;//obraz się powiększa po kolejnych dropach
                myBitmapImage.DecodePixelWidth = (int)image1.ActualWidth;
                myBitmapImage.EndInit();
                image1.Source = myBitmapImage;
                myBitmapImage.UriSource = null;
                myBitmapImage = null;
                //
                isImageDropped = true;
                //
            }
            catch (Exception e)
            {
                retVal = false;
                MessageBox.Show(e.Message, "showFile");
            }
            finally
            {
                win1.Cursor = Cursors.Arrow;
                image1.Visibility = System.Windows.Visibility.Visible;
                win1.IsEnabled = true;
                image1.Refresh();
            }
            return retVal;
        }

        //====================================================================================================================

        private bool isValidImageFile(string filename)
        {
            string ext = System.IO.Path.GetExtension(filename).ToUpper();
            return ".JPG" == ext || ".TIFF" == ext || ".TIF" == ext || ".PNG" == ext;
        }
        private bool isValidTextFile(string filename)
        {
            string ext = System.IO.Path.GetExtension(filename).ToUpper();
            return ".TXT" == ext;
        }
        //====================================================================================================================

        private bool isJpg(string fileName)
        {
            using (System.IO.Stream originalFile = System.IO.File.Open(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
            {
                // lossless
                BitmapDecoder original = BitmapDecoder.Create(originalFile, BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.None);

                if (original.CodecInfo.FileExtensions.Contains("jpg"))
                {
                    originalFile.Close();
                    return true;
                }
            }
            return false;
        }
        private bool isTiff(string fileName)
        {
            using (System.IO.Stream originalFile = System.IO.File.Open(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
            {
                // lossless
                BitmapDecoder original = BitmapDecoder.Create(originalFile, BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.None);

                if (original.CodecInfo.FileExtensions.Contains("tif"))
                {
                    originalFile.Close();
                    return true;
                }
            }
            return false;
        }
        //====================================================================================================================


    }
}
